package com.core.family.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.core.family.admin.domain.po.PlatStorage;
import com.core.family.admin.service.PlatStorageService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.core.family.admin.mapper.PlatStorageLogicMapper;
import com.core.family.admin.domain.po.PlatStorageLogic;
import com.core.family.admin.service.PlatStorageLogicService;
import com.core.common.gen.BaseServiceImpl;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;


/**
 * @author honglei
 * @version 1.0
 * @time: 2023-6-14 17:05:08
 * @Description 服务实现层 文件逻辑存储
 */
@Slf4j
@Service
public class PlatStorageLogicServiceImpl extends BaseServiceImpl<PlatStorageLogicMapper, PlatStorageLogic> implements PlatStorageLogicService {

    @Autowired
    PlatStorageService platStorage;

    @SneakyThrows
    @Override
    public PlatStorageLogic saveOrUpdateFile(MultipartFile file,Integer id) {
        String name=file.getName();
        String fileName=file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String relatePath=suffixName.replace(".","");
        String contentType=file.getContentType();
        long size=file.getSize();
        long size2=file.getBytes().length;
        PlatStorage storage = platStorage.saveOrUpdate(file.getInputStream(),size,suffixName);

        PlatStorageLogic storageLogic=new PlatStorageLogic()
                .setId(id)
                .setFileName(fileName)
                .setStorageId(storage.getId())
                .setFileUrl(storage.getFileUrl())
                .setFileKey(storage.getFileKey());

        this.saveOrUpdate(storageLogic);

        return storageLogic;
    }

    @Override
    public PlatStorageLogic getByIdOrkeyName(String keyName, String id) {
        LambdaQueryWrapper<PlatStorageLogic> wrapper=new LambdaQueryWrapper();
        wrapper.eq(Objects.nonNull(keyName),PlatStorageLogic::getFileKey,keyName)
        .eq(Objects.nonNull(id),PlatStorageLogic::getId,id);
        return getOne(wrapper);

    }
}

